{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "| [02_advanced/01_系统交互_os.ipynb](https://github.com/shibing624/python-tutorial/blob/master/02_advanced/01_系统交互_os.ipynb)  | Python系统交互操作  |[Open In Colab](https://colab.research.google.com/github/shibing624/python-tutorial/blob/master/02_advanced/01_系统交互_os.ipynb) |\n",
    "\n",
    "# os模块\n",
    "\n",
    "Python的内置os模块，是与操作系统进行交互的模块，主要有如下功能：\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "## 文件路径操作\n",
    "- os.remove(path) 或 os.unlink(path) ：删除指定路径的文件。路径可以是全名，也可以是当前工作目录下的路径。\n",
    "- os.removedirs：删除文件，并删除中间路径中的空文件夹\n",
    "- os.chdir(path)：将当前工作目录改变为指定的路径\n",
    "- os.getcwd()：返回当前的工作目录\n",
    "- os.curdir：表示当前目录的符号\n",
    "- os.rename(old, new)：重命名文件\n",
    "- os.renames(old, new)：重命名文件，如果中间路径的文件夹不存在，则创建文件夹\n",
    "- os.listdir(path)：返回给定目录下的所有文件夹和文件名，不包括 '.' 和 '..' 以及子文件夹下的目录。（'.' 和 '..' 分别指当前目录和父目录）\n",
    "- os.mkdir(name)：产生新文件夹\n",
    "- os.makedirs(name)：产生新文件夹，如果中间路径的文件夹不存在，则创建文件夹\n",
    "\n",
    "\n",
    "导入该模块："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "产生文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "f = open('test.file', 'w')\n",
    "f.close()\n",
    "print('test.file' in os.listdir(os.curdir))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "重命名文件:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "os.rename(\"test.file\", \"test.new.file\")\n",
    "print(\"test.file\" in os.listdir(os.curdir))\n",
    "print(\"test.new.file\" in os.listdir(os.curdir))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除文件\n",
    "os.remove(\"test.new.file\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 系统常量\n",
    "- windows 为 \\r\\n\n",
    "- unix为 \\n\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.linesep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当前操作系统的路径分隔符：\n",
    "os.sep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当前操作系统的环境变量中的分隔符（';' 或 ':'）：\n",
    "- windows 为 ;\n",
    "- unix 为:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "':'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.pathsep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "os.environ 是一个存储所有环境变量的值的字典，可以修改。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "environ{'PATH': '/Users/xuming/opt/anaconda3/bin:/Users/xuming/opt/anaconda3/condabin:/Users/xuming/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',\n",
       "        'CONDA_DEFAULT_ENV': 'base',\n",
       "        'CONDA_EXE': '/Users/xuming/opt/anaconda3/bin/conda',\n",
       "        'CONDA_PYTHON_EXE': '/Users/xuming/opt/anaconda3/bin/python',\n",
       "        'LANG': 'en_US.UTF-8',\n",
       "        'COMMAND_MODE': 'unix2003',\n",
       "        'CONDA_PREFIX': '/Users/xuming/opt/anaconda3',\n",
       "        'AUTOJUMP_SOURCED': '1',\n",
       "        '_CE_M': '',\n",
       "        'LOGNAME': 'xuming',\n",
       "        'XPC_SERVICE_NAME': 'application.com.jetbrains.pycharm.494262.495021',\n",
       "        'PWD': '/Users/xuming/PycharmProjects/python-tutorial',\n",
       "        'CONDA_SHLVL': '1',\n",
       "        '__CFBundleIdentifier': 'com.jetbrains.pycharm',\n",
       "        'LANGUAGE': '',\n",
       "        'PYTHONPATH': '/Users/xuming/PycharmProjects/python-tutorial',\n",
       "        'SHELL': '/bin/zsh',\n",
       "        'PAGER': 'cat',\n",
       "        'LSCOLORS': 'Gxfxcxdxbxegedabagacad',\n",
       "        'SECURITYSESSIONID': '187bb',\n",
       "        'OLDPWD': '/',\n",
       "        'USER': 'xuming',\n",
       "        'ZSH': '/Users/xuming/.oh-my-zsh',\n",
       "        'TMPDIR': '/var/folders/my/wtb78vc53sv_jjmg5pk2j1_c0000gn/T/',\n",
       "        'LaunchInstanceID': '77CFE9AC-A411-41D1-9183-E6A49550A179',\n",
       "        'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.SBzaQzFxx3/Listeners',\n",
       "        '_CE_CONDA': '',\n",
       "        'XPC_FLAGS': '0x0',\n",
       "        'LC_ALL': 'en_US.UTF-8',\n",
       "        '__CF_USER_TEXT_ENCODING': '0x1F5:0x19:0x34',\n",
       "        'CONDA_PROMPT_MODIFIER': '(base) ',\n",
       "        'LESS': '-R',\n",
       "        'LC_CTYPE': 'zh_CN.UTF-8',\n",
       "        'HOME': '/Users/xuming',\n",
       "        'AUTOJUMP_ERROR_PATH': '/Users/xuming/Library/autojump/errors.log',\n",
       "        'JPY_PARENT_PID': '59863',\n",
       "        'TERM': 'xterm-color',\n",
       "        'CLICOLOR': '1',\n",
       "        'GIT_PAGER': 'cat',\n",
       "        'MPLBACKEND': 'module://ipykernel.pylab.backend_inline'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.environ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# os.path 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- os.path.isfile(path) ：检测一个路径是否为普通文件\n",
    "- os.path.isdir(path)：检测一个路径是否为文件夹\n",
    "- os.path.exists(path)：检测路径是否存在\n",
    "- os.path.isabs(path)：检测路径是否为绝对路径\n",
    "\n",
    "windows系统："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(os.path.isfile(\"C:/Windows\"))\n",
    "print(os.path.isdir(\"C:/Windows\"))\n",
    "print(os.path.exists(\"C:/Windows\"))\n",
    "print(os.path.isabs(\"C:/Windows\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "unix系统："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(os.path.isfile(\"/Users\"))\n",
    "print(os.path.isdir(\"/Users\"))\n",
    "print(os.path.exists(\"/Users\"))\n",
    "print(os.path.isabs(\"/Users\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## split 和 join\n",
    "- os.path.split(path)：拆分一个路径为 (head, tail) 两部分\n",
    "- os.path.join(a, *p)：使用系统的路径分隔符，将各个部分合成一个路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:/tem b.txt\n"
     ]
    }
   ],
   "source": [
    "head, tail = os.path.split(\"c:/tem/b.txt\")\n",
    "print(head, tail)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c:/tem/b.txt'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = \"c:/tem\"\n",
    "b = \"b.txt\"\n",
    "os.path.join(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_files(dir_path):\n",
    "    '''\n",
    "    列出文件夹下的所有文件\n",
    "    :param dir_path: 父文件夹路径\n",
    "    :return: \n",
    "    '''\n",
    "    for parent, dirname, filenames in os.walk(dir_path):\n",
    "        for filename in filenames:\n",
    "            print(\"parent is:\", parent)\n",
    "            print(\"filename is:\", filename)\n",
    "            print(\"full name of the file is:\", os.path.join(parent, filename))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列出当前文件夹的所有文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "parent is: .\n",
      "filename is: 07_装饰器.ipynb\n",
      "full name of the file is: ./07_装饰器.ipynb\n",
      "parent is: .\n",
      "filename is: 05.generator.py\n",
      "full name of the file is: ./05.generator.py\n",
      "parent is: .\n",
      "filename is: 06.with.py\n",
      "full name of the file is: ./06.with.py\n",
      "parent is: .\n",
      "filename is: 01_系统交互_os.ipynb\n",
      "full name of the file is: ./01_系统交互_os.ipynb\n",
      "parent is: .\n",
      "filename is: 07.decorator.py\n",
      "full name of the file is: ./07.decorator.py\n",
      "parent is: .\n",
      "filename is: 08.compile.py\n",
      "full name of the file is: ./08.compile.py\n",
      "parent is: .\n",
      "filename is: 03.function.py\n",
      "full name of the file is: ./03.function.py\n",
      "parent is: .\n",
      "filename is: 08_面向对象编程.ipynb\n",
      "full name of the file is: ./08_面向对象编程.ipynb\n",
      "parent is: .\n",
      "filename is: 05_生成器.ipynb\n",
      "full name of the file is: ./05_生成器.ipynb\n",
      "parent is: .\n",
      "filename is: 04_迭代器.ipynb\n",
      "full name of the file is: ./04_迭代器.ipynb\n",
      "parent is: .\n",
      "filename is: 02.sql.py\n",
      "full name of the file is: ./02.sql.py\n",
      "parent is: .\n",
      "filename is: 02_数据库_sql.ipynb\n",
      "full name of the file is: ./02_数据库_sql.ipynb\n",
      "parent is: .\n",
      "filename is: 03_高阶函数.ipynb\n",
      "full name of the file is: ./03_高阶函数.ipynb\n",
      "parent is: .\n",
      "filename is: 06_上下文管理器.ipynb\n",
      "full name of the file is: ./06_上下文管理器.ipynb\n",
      "parent is: .\n",
      "filename is: test.db\n",
      "full name of the file is: ./test.db\n",
      "parent is: .\n",
      "filename is: 04.iterator.py\n",
      "full name of the file is: ./04.iterator.py\n",
      "parent is: ./03_oop\n",
      "filename is: 02.property.py\n",
      "full name of the file is: ./03_oop/02.property.py\n",
      "parent is: ./03_oop\n",
      "filename is: 01.init.py\n",
      "full name of the file is: ./03_oop/01.init.py\n",
      "parent is: ./03_oop\n",
      "filename is: 04.inheritance.py\n",
      "full name of the file is: ./03_oop/04.inheritance.py\n",
      "parent is: ./03_oop\n",
      "filename is: 03.forest.py\n",
      "full name of the file is: ./03_oop/03.forest.py\n",
      "parent is: ./03_oop\n",
      "filename is: 05.interface.py\n",
      "full name of the file is: ./03_oop/05.interface.py\n",
      "parent is: ./03_oop\n",
      "filename is: 06.methodtype.py\n",
      "full name of the file is: ./03_oop/06.methodtype.py\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 04_迭代器-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/04_迭代器-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 06_上下文管理器-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/06_上下文管理器-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 08_面向对象编程-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/08_面向对象编程-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 05_生成器-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/05_生成器-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 02_数据库_sql-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/02_数据库_sql-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 07_装饰器-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/07_装饰器-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 01_系统交互_os-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/01_系统交互_os-checkpoint.ipynb\n",
      "parent is: ./.ipynb_checkpoints\n",
      "filename is: 03_高阶函数-checkpoint.ipynb\n",
      "full name of the file is: ./.ipynb_checkpoints/03_高阶函数-checkpoint.ipynb\n",
      "parent is: ./04_thread\n",
      "filename is: condition_demo.py\n",
      "full name of the file is: ./04_thread/condition_demo.py\n",
      "parent is: ./04_thread\n",
      "filename is: get_thread_result.py\n",
      "full name of the file is: ./04_thread/get_thread_result.py\n",
      "parent is: ./04_thread\n",
      "filename is: spider_thread.py\n",
      "full name of the file is: ./04_thread/spider_thread.py\n",
      "parent is: ./04_thread\n",
      "filename is: thread_demo.py\n",
      "full name of the file is: ./04_thread/thread_demo.py\n",
      "parent is: ./04_thread\n",
      "filename is: file_thread_demo.py\n",
      "full name of the file is: ./04_thread/file_thread_demo.py\n",
      "parent is: ./04_thread\n",
      "filename is: logger.py\n",
      "full name of the file is: ./04_thread/logger.py\n",
      "parent is: ./04_thread\n",
      "filename is: operation.py\n",
      "full name of the file is: ./04_thread/operation.py\n",
      "parent is: ./04_thread\n",
      "filename is: time_limit.py\n",
      "full name of the file is: ./04_thread/time_limit.py\n",
      "parent is: ./04_thread\n",
      "filename is: print_time.py\n",
      "full name of the file is: ./04_thread/print_time.py\n",
      "parent is: ./04_thread\n",
      "filename is: async.py\n",
      "full name of the file is: ./04_thread/async.py\n",
      "parent is: ./04_thread\n",
      "filename is: timer_demo.py\n",
      "full name of the file is: ./04_thread/timer_demo.py\n",
      "parent is: ./04_thread\n",
      "filename is: file_thread_demo_linux.py\n",
      "full name of the file is: ./04_thread/file_thread_demo_linux.py\n"
     ]
    }
   ],
   "source": [
    "dir = os.curdir\n",
    "get_files(dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Byte Code 编译\n",
    "Python, Java 等语言先将代码编译为 byte code（不是机器码），然后再处理：\n",
    "> .py -> .pyc -> interpreter\n",
    "\n",
    "eval(statement, glob, local)\n",
    "\n",
    "使用 eval 函数动态执行代码，返回执行的值。\n",
    "\n",
    "exec(statement, glob, local)\n",
    "\n",
    "使用 exec 可以添加修改原有的变量:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "exec('b = a + 10')\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 2, 'b': 3}\n"
     ]
    }
   ],
   "source": [
    "local = dict(a=2)\n",
    "glob = {}\n",
    "exec(\"b = a+1\", glob, local)\n",
    "\n",
    "print(local)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "compile 函数生成 byte code：\n",
    "compile(str, filename, mode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = compile('a+2', '', 'eval')\n",
    "print(eval(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "c = compile(\"b=a+4\", \"\", 'exec')\n",
    "exec(c)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Expression(body=BinOp(left=Name(id='a', ctx=Load()), op=Add(), right=Constant(value=10, kind=None)))\""
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# abstract syntax trees\n",
    "import ast\n",
    "\n",
    "tree = ast.parse('a+10', '', 'eval')\n",
    "ast.dump(tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "c = compile(tree, '', 'eval')\n",
    "d = eval(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10.0, 2, True, 'foo']\n"
     ]
    }
   ],
   "source": [
    "# 安全的使用方法 literal_eval ，只支持基本值的操作：\n",
    "b = ast.literal_eval('[10.0, 2, True, \"foo\"]')\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本节完。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}